<template>
  <div class="main">
    <div class="title">
      <a class="active" href="/login">登录</a>
      <span>·</span>
      <a href="/register">注册</a>
    </div>
    <div class="sign-up-container">
      <el-form ref="userForm" :model="user" prop="upUserForm">
        <el-form-item class="input-prepend restyle" prop="userId" :rules="[{ required: true, message: '请输入学号或学工号', trigger: 'blur' },{validator: checkPhone, trigger: 'blur'}]">
          <div >

            <el-input v-if="radio == '1'" type="text" placeholder="学号" v-model="user.userId"/>
            <el-input v-else type="text" placeholder="学工号" v-model="user.userId"/>
            <i class="iconfont icon-user" ></i>
          </div>
        </el-form-item>

        <el-form-item class="input-prepend" prop="password" :rules="[{ required: true, message: '请输入密码', trigger: 'blur' }, {min: 6, max: 14, message: '密码长度在 6-14 位', trigger: 'blur'}]">
          <div>
            <el-input type="password" placeholder="密码" v-model="user.password"/>
            <i class="iconfont icon-password"></i>
          </div>
        </el-form-item>

        <div class="btn">
          <input type="button" class="sign-in-button" value="登录" @click="submitLogin()">
        </div>
      </el-form>
      <!-- 更多登录方式 -->
      <div class="more-sign">
        <h2>-----------------------身份选择-----------------------</h2>
        <el-row style="margin-top: 20px;">
          <el-radio v-model="radio" label="1" style="margin-right: 30px;">学生登录</el-radio>
          <el-radio v-model="radio" label="2" style="margin-left: 30px;">教师登录</el-radio>
        </el-row>
      </div>
    </div>

  </div>
</template>

<script>
import '~/assets/css/sign.css'
import '~/assets/css/iconfont.css'

import cookie from 'js-cookie'
import loginApi from '@/api/login'

export default {
  layout: 'sign',
  data () {
    return {
      //封装登录手机号和密码对象
      loading: false,
      radio: '1',
      user:{
        userId:'',
        password:''
      },
      selectUser: true,
      //用户信息
      loginInfo:{}
    }
  },
  methods: {
    // 登录的方法
    selectStu() {
      console.log(this.selectUser)
    },
    selectTea (){
      console.log(this.selectUser)
    },
    stuLogin() {
      this.loading = true;
      loginApi.submitLogin(this.user) 
          .then(response => {
            // 第二步 获取token字符串放到cookie里面
            // 第一个参数cookie名称，第二个参数值，第三个参数作用范围
            // cookie.set('guli_token',response.data.data.token,{domain: 'localhost'})
            let loginRes = response.data
            if (loginRes.success) {
              cookie.set('token',response.data.data.token,{domain: 'localhost'})
              console.log('cookie: ', cookie.get('token'))
              loginApi.getMemberInfo()
              .then(response =>{
                this.loginInfo = response.data.data
                cookie.set('userInfo',this.loginInfo,{domain: 'localhost'})
                cookie.set('isTea',0,{domain: 'localhost'})
                console.log('getStuInfo: ', cookie.get('userInfo'))
                this.$message({
                  type: 'success',
                  message: "登录成功"
                })
                this.loading=false
                this.$router.push({path:'/'})
              })
            }
            else{
              this.loading=false
              this.user.password = ''
              this.$message({
                  type: 'error',
                  message: "帐号或密码错误"
                })
            }
          })
    },
    teaLogin(){
      this.loading = true;
      loginApi.submitTeaLogin(this.user) 
          .then(response => {
            // 第二步 获取token字符串放到cookie里面
            // 第一个参数cookie名称，第二个参数值，第三个参数作用范围
            // cookie.set('guli_token',response.data.data.token,{domain: 'localhost'})
            let loginRes = response.data
            console.log('teaLogin', loginRes)
            if (loginRes.success) {
              cookie.set('token',response.data.data.token,{domain: 'localhost'})
              console.log('cookie: ', cookie.get('token'))
              loginApi.getTeaInfo()
              .then(response =>{
                this.loginInfo = response.data.data
                cookie.set('userInfo',this.loginInfo,{domain: 'localhost'})
                cookie.set('isTea',1,{domain: 'localhost'})
                console.log('getTeaInfo: ', cookie.get('userInfo'))
                this.$message({
                  type: 'success',
                  message: "登录成功"
                })
                this.loading=false
                this.$router.push({path:'/'})
              })
            }
            else{
              this.loading=false
              this.user.password = ''
              this.$message({
                  type: 'error',
                  message: "帐号或密码错误"
                })
            }
          })
    },
    submitLogin() {
      // 第一步 调用接口进行登录，返回token字符串
      if(this.radio === "1"){
        console.log('stuLogin')
        this.stuLogin()
      }
      else{
        console.log('teaLogin')
        this.teaLogin()
      }
    },
    checkPhone (rule, value, callback) {
      //debugger
      // if (!(/^1[34578]\d{9}$/.test(value))) {
      //   return callback(new Error('手机号码格式不正确'))
      // }
      return callback()
    }
  }
}
</script>
<style>
   .el-form-item__error{
    z-index: 9999999;
  }
</style>